#include "SceneModel/SceneModelManipulator.h"

namespace GCRE {
	namespace SceneModel {

		CSceneModelManipulator::CSceneModelManipulator()
		{

		}

		CSceneModelManipulator::~CSceneModelManipulator()
		{

		}

		EntityPtr CSceneModelManipulator::CreateEntity( const BasicType::SFString& strType )
		{
			RuntimeClass* pClass = RuntimeClass::m_pFirstClass;
			while( pClass != NULL )
			{
				if ( strType == pClass->m_strClassName )
					break;
				pClass = pClass->m_pNextClass;
			}
			IEntity* pEntity = NULL;
			if ( pClass && pClass->m_pfnCreateEntity != NULL )
				pEntity = pClass->m_pfnCreateEntity();
			return pEntity;
		}

		BasicType::SFBool CSceneModelManipulator::SaveEntity( const EntityPtr& pEntity )
		{
			IDToEntityPtrIterator iterIDToEntityPtrEnd = m_IDToEntityPtr.end();
			IDToEntityPtrIterator iterIDToEntityPtr = m_IDToEntityPtr.find( pEntity->GetID() );
			if ( iterIDToEntityPtr != iterIDToEntityPtrEnd )
			{
				printf( "Entity Exist!\n" );
				return false;
			}
			m_IDToEntityPtr[pEntity->GetID()] = pEntity;

			StringToEntityPtrIterator iterStringToEntityPtrEnd = m_StringToEntityPtr.end();
			StringToEntityPtrIterator iterStringToEntityPtr = m_StringToEntityPtr.find( pEntity->GetName() );
			if ( iterStringToEntityPtr != iterStringToEntityPtrEnd )
			{
				printf( "Entity Exist!\n" );
				return false;
			}
			m_StringToEntityPtr[pEntity->GetName()] = pEntity;

			return true;
		}

		EntityPtr CSceneModelManipulator::FindEntityByID( BasicType::SFUInt id ) const
		{
			IDToEntityPtrIterator iterIDToEntityPtr = m_IDToEntityPtr.find( id );
			IDToEntityPtrIterator iterIDToEntityPtrEnd = m_IDToEntityPtr.end();
			if ( iterIDToEntityPtr != iterIDToEntityPtrEnd )
				return iterIDToEntityPtr->second;
			else
				return NULL;
		}

		EntityPtr CSceneModelManipulator::FindEntityByName( const BasicType::SFString& name ) const
		{
			StringToEntityPtrIterator iterStringToEntityPtrEnd = m_StringToEntityPtr.end();
			StringToEntityPtrIterator iterStringToEntityPtr = m_StringToEntityPtr.find( name );
			if ( iterStringToEntityPtr != iterStringToEntityPtrEnd )
				return iterStringToEntityPtr->second;
			else
				return NULL;
		}
	}
}